package com.webBasic.controller;

import com.alibaba.fastjson.JSONObject;
import com.webBasic.Result.Result;
import com.webBasic.Util.HttpUtil;
import com.webBasic.constant.UserConstant;
import com.webBasic.domain.User;
import com.webBasic.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author  王凯飞
 * @date:2019/10/29
 * 用户登陆信息
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String hello(){
        return "index";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Result login(@RequestBody JSONObject param,
                        HttpServletRequest request,
                        HttpServletResponse resp,
                        Model model){
        String userName = param.getString("userName");
        String password = param.getString("password");

        boolean flag = userService.selectUserByUserName(userName);
        if(!flag){
            return UserConstant.INVALID_USER;
        }

        User paramUser = new User();
        paramUser.setUserName(userName);
        paramUser.setPassword(password);
        HashMap user = userService.selectUserByUserNameAndPassword(paramUser);
        if(user == null){
            return UserConstant.INVALID_PASSWORD;
        }

        request.getSession().setAttribute("user",user);

        HttpUtil.addCookie(resp,"user",userName);

        model.addAttribute("user",user);
        return UserConstant.LOGIN_SUCCESS;
    }

    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(HttpServletRequest request){
        request.getSession().invalidate();
        return "redirect:loginInput";
    }

    @RequestMapping(value = "/ssologin",method = RequestMethod.GET)
    @ResponseBody
    public Result ssoLogin(@RequestParam("userName")String userName,
                           @RequestParam("password")String password,
                           HttpServletRequest request,
                           HttpServletResponse resp,
                           Model model){
        if(userName == null){
            String callback = request.getParameter("callback");
            System.out.println(callback);
            return UserConstant.INVALID_USER;
        }


        boolean flag = userService.selectUserByUserName(userName);
        if(!flag){
            return UserConstant.INVALID_USER;
        }

        User paramUser = new User();
        paramUser.setUserName(userName);
        paramUser.setPassword(null);
        HashMap user = userService.selectUserByUserNameAndPassword(paramUser);
        if(user == null){
            return UserConstant.INVALID_PASSWORD;
        }

        request.getSession().setAttribute("user",user);

        HttpUtil.addCookie(resp,"user",userName);

        model.addAttribute("user",user);
        return UserConstant.LOGIN_SUCCESS;
    }
}
